<?php

namespace plugin\ycMassage\app\shop\controller;

use app\common\builder\FormBuilder;
use app\common\builder\ListBuilder;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageShop;
use plugin\ycMassage\app\model\MassageShopThumb;
use plugin\ycMassage\utils\enum\Examine;
use support\Request;
use think\facade\Db;

class ShopThumbController extends BaseController
{
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->addActionOptions('操作', [
            'width' => 160
        ]);
        $builder->addScreen('name', 'input', '门店', [
            'placeholder' => '门店搜索'
        ]);
        $builder->addScreen('examine', 'select', '状态', [
            'options' => Examine::getOptions()
        ]);
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addTopButton('add', '添加门店图片', [
            'api'       => $this->pluginPrefix . '/shop/ShopThumb/create',
            'path' => '/ShopThumb/create',
            'type'      => 'modal'
        ], [
            'title' => '添加门店图片',
        ], [
            'type' => 'success'
        ]);
        $builder->addRightButton('examine', '审核', [
            'api'    => $this->pluginPrefix . '/shop/ShopThumb/examine',
            'path' => '/ShopThumb/examine',
            'type'      => 'modal',
            'params' => [
                'field' => 'examine',
                'where' => '=',
                'value' => Examine::WAIT['value']
            ]
        ], [], [
            'type' => 'danger',
            'link' => true
        ]);
        $builder->addRightButton('del', '删除', [
            'type'   => 'confirm',
            'api'    => $this->pluginPrefix . '/shop/ShopThumb/delete',
            'method' => 'delete'
        ], [
            'title'   => '温馨提示',
            'content' => '是否确认删除该数据',
        ], [
            'type' => 'error',
            'link' => true
        ]);
        $builder->addColumn('id', '序号', [
            'width' => 90
        ])
            ->addColumn('title', '门店', [])
            ->addColumnEle('url', '图片', [
                'params' => [
                    'type' => 'image',
                    'props'=>[
                        'preview'=>true
                    ]
                ],
            ])
            ->addColumnEle('examine', '状态', [
                'width' => 100,
                'params' => [
                    'type' => 'switch',
                    'api' => $this->pluginPrefix . '/shop/ShopThumb/updateState',
                    'checked' => Examine::YES,
                    'unchecked' => Examine::WAIT
                ],
            ]);
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['img.saas_appid', '=', $request->saas_appid]
        ];
        $shop_id = $request->get('shop_id');
        if ($shop_id) {
            $where[] = ['img.shop_id', '=', $shop_id];
        }
        $name = $request->get('name');
        if ($name) {
            $where[] = ['m.title', 'like', "%{$name}%"];
        }
        $examine = $request->get('examine');
        if ($examine !== null) {
            $where[] = ['img.examine', '=', $examine];
        }
        $where[] = ['m.mch_id', '=', $request->use_mch_id];
        $data = MassageShopThumb::where($where)
            ->alias('img')
            ->join('MassageShop m', 'm.id = img.shop_id')
            ->order('img.create_time desc')
            ->field('img.*,m.title')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
            });
        return $this->successRes($data);
    }
    public function create(Request $request)
    {
        $saas_appid = $request->saas_appid;
        if ($request->method() === 'POST') {
            $D = $request->post();
            if (empty($D['thumb'])) {
                return $this->fail('请上传门店图片');
            }
            Db::startTrans();
            try {
                $shop_id = $D['shop_id'];
                $MassageShop = MassageShop::where(['id' => $shop_id, 'mch_id' => $request->use_mch_id, 'saas_appid' => $request->saas_appid])->find();
                if (!$MassageShop) {
                    return $this->fail('门店不存在');
                }
                $MassageShopThumb = new MassageShopThumb;
                $thumb = [];
                foreach ($D['thumb'] as $key => $value) {
                    $temp = [
                        'shop_id' => $MassageShop->id,
                        'saas_appid' => $saas_appid,
                        'url' => $value,
                    ];
                    krsort($temp);
                    $thumb[] = $temp;
                }
                $MassageShopThumb->saveAll($thumb);
                Db::commit();
                return $this->success('添加成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }

        $builder = new FormBuilder;
        $builder->setMethod('POST');
        $builder->addRow('shop_id', 'input', '门店ID', '', [
            'col'           => 12
        ]);
        $builder->addComponent('thumb', 'uploadify', '门店图片', '', [
            'col' => 24,
            'props' => [
                'type' => 'image',
                'limit' => 99,
                'format' => ['jpg', 'jpeg', 'png']
            ],
        ]);
        $data = $builder->create();
        return $this->successRes($data);
    }
    public function examine(Request $request)
    {
        $id = $request->get('id');
        $MassageShopThumb = MassageShopThumb::where(['id' => $id, 'saas_appid' => $request->saas_appid, 'examine' => Examine::WAIT['value']])->find();
        if (!$MassageShopThumb) {
            return $this->fail('数据不存在');
        }
        $MassageShop = MassageShop::where(['id' => $MassageShopThumb->shop_id, 'mch_id' => $request->use_mch_id, 'saas_appid' => $request->saas_appid])->find();
        if (!$MassageShop) {
            return $this->fail('门店不存在');
        }
        if ($request->method() === 'POST') {
            $D = $request->post();
            $MassageShopThumb->examine = $D['examine'];
            Db::startTrans();
            try {
                if ($MassageShopThumb->examine == Examine::NO['value']) {
                    $MassageShopThumb->delete();
                } else {
                    $MassageShopThumb->save();
                }
                Db::commit();
                return $this->success('审核成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }

        $builder = new FormBuilder;
        $builder->setMethod('POST');
        $builder->addRow('examine', 'radio', '状态', Examine::YES['value'], [
            'col' => 24,
            'options' => Examine::getOptions()
        ]);
        $builder->addComponent('url', 'el-image', '门店图片', '', [
            'col'           => 24,
            'props'         => [
                'src'       => $MassageShopThumb->url,
                'width'     => 160,
                'height'    => 220
            ]
        ]);
        $data = $builder->setData($MassageShopThumb)->create();
        return $this->successRes($data);
    }
    public function updateState(Request $request)
    {
        $id = $request->post('id');
        $MassageShopThumb = MassageShopThumb::where(['id' => $id, 'saas_appid' => $request->saas_appid])->find();
        if (!$MassageShopThumb) {
            return $this->fail('数据不存在');
        }
        $MassageShop = MassageShop::where(['id' => $MassageShopThumb->shop_id, 'mch_id' => $request->use_mch_id, 'saas_appid' => $request->saas_appid])->find();
        if (!$MassageShop) {
            return $this->fail('门店不存在');
        }
        $MassageShopThumb->examine = $MassageShopThumb->examine == Examine::YES['value'] ? Examine::NO['value'] : Examine::YES['value'];
        if ($MassageShopThumb->save()) {
            return $this->success('修改成功');
        }
        return $this->fail('修改失败');
    }
    public function delete(Request $request)
    {
        $id = $request->post('id');
        $where = [
            ['saas_appid', '=', $request->saas_appid],
            ['id', '=', $id]
        ];
        $MassageShopThumb = MassageShopThumb::where($where)->find();
        if (!$MassageShopThumb) {
            return $this->fail('数据不存在');
        }
        $MassageShop = MassageShop::where(['id' => $MassageShopThumb->shop_id, 'mch_id' => $request->use_mch_id, 'saas_appid' => $request->saas_appid])->find();
        if (!$MassageShop) {
            return $this->fail('门店不存在');
        }
        if ($MassageShopThumb->delete()) {
            return $this->success('删除成功');
        }
        return $this->fail('删除失败');
    }
}
